package com.example.mytestdemo.softkey;

import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * Author by liuzhiyou on 2021/11/15.
 * PS: 拖拽移动
 */
public class DragMoveHelper {

    private View touchView;
    private View moveView;

    private int containerWidth;
    private int containerHeight;

    private final PointF pointF = new PointF();

    public DragMoveHelper(View touchView, final View moveView) {
        this.touchView = touchView;
        this.moveView = moveView;
        moveView.post(new Runnable() {
            @Override
            public void run() {
                pointF.set(moveView.getX(), moveView.getY());
            }
        });
    }

    public void bind() {
        touchView.setOnTouchListener(new View.OnTouchListener() {
            int lastX, lastY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = x;
                        lastY = y;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int offsetX = x - lastX;
                        int offsetY = y - lastY;
                        moveView(offsetX, offsetY);
                        break;
                }
                return true;
            }
        });

    }

    /**
     * 重置拖动后的坐标为默认展示坐标点
     */
    public void reset() {
        moveView.setX(pointF.x);
        moveView.setY(pointF.y);
    }

    private void moveView(int offsetX, int offsetY) {
        ViewGroup container = (ViewGroup) moveView.getParent();
        containerWidth = container.getWidth();
        containerHeight = container.getHeight();

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) moveView.getLayoutParams();
        int marginLeft = params.leftMargin;
        int marginRight = params.rightMargin;
        int marginTop = params.topMargin;
        int marginBottom = params.bottomMargin;

        float newX = moveView.getX() + offsetX;
        float newY = moveView.getY() + offsetY;
        int width = moveView.getWidth();
        int height = moveView.getHeight();
        //水平方向
        if (Math.abs(newX) > 1100) {//left
            newX = -1100;
        } else if (newX > containerWidth - moveView.getWidth() - marginRight) {//right
            newX = containerWidth - moveView.getWidth() - marginRight;
        }
        //垂直方向
        if (Math.abs(newY) > 760) {//top
            newY = -760;
        } else if (newY > containerHeight - moveView.getHeight() - marginBottom) {//bottom
            newY = containerHeight - moveView.getHeight() - marginBottom;
        }

        moveView.setX(newX);
        moveView.setY(newY);

        Log.d("tgw move", "moveView: "+moveView.getX()+"----y:"+moveView.getY());
        Log.d("tgw newX move", "moveView: "+newX+"----y:"+newY);
    }
}
